package com.example.petplanet.config;

import com.example.petplanet.intercept.TokenIntercept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    private final TokenIntercept tokenIntercept;


    @Autowired
    WebConfig(TokenIntercept tokenIntercept){
        this.tokenIntercept=tokenIntercept;
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:5173","http://localhost:8080")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenIntercept)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/user/LoginByPhone",
                        "/content/callBack",
                        "/content/getRecommendContents",
                        "/user/sendPhoneCode"
                );
    }

}
